package com.aaa.service.fpa.impl;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.aspectFPA.InsertAnnFPA;
import com.aaa.aspectFPA.UpdateAnnFPA;
import com.aaa.dao.fpa.PersonalityTestPlanDao;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.fpa.TestPlanFpa;
import com.aaa.service.fpa.PersonalityTestPlanService;
import com.aaa.utils.MyTools;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：chenang（731712841@qq.com）
 * @date ：Created in 2022/9/1 17:06
 * @description：性格测试,测试计划实现类 tbl_test_plan_fpa
 * @modified By：
 * @version:
 */
@Log
@Service
public class PersonalityTestPlanServiceImpl  implements PersonalityTestPlanService {
    @Autowired
    PersonalityTestPlanDao personalityTestPlanDao;
    /**
     *      分页排序,全部查询
     * @param pageSortEntity
     * @param personalityTestPlanEntity
     * @return
     */
    @Override
    public Page queryPersonalityTestPlanAll(PageSortEntity pageSortEntity, TestPlanFpa personalityTestPlanEntity) {
        PageHelper.startPage(pageSortEntity.getPage(),pageSortEntity.getLimit(), pageSortEntity.getOrderString());
        List<TestPlanFpa> mapList = personalityTestPlanDao.queryPersonalityTestPlanAll(personalityTestPlanEntity);
        Page page = (Page) mapList;
        return page;
    }

    /**
     *   多行删除
     * @param id
     * @return
     */
    @Override
    public int deletequeryPersonalityTestPlan(int[] id) {
        ArrayList<Object> arrayList = new ArrayList<>();
        for( int i : id ){
            arrayList.add(i);
        }
        int i = personalityTestPlanDao.deletequeryPersonalityTestPlan(arrayList);
        return i;
    }

    /**
     *      修改
     * @param personalityTestPlanEntity
     * @return
     */
    @UpdateAnnFPA
    @Override
    public int updatePersonalityTestPlan(TestPlanFpa personalityTestPlanEntity) {
        return personalityTestPlanDao.updatePersonalityTestPlan(personalityTestPlanEntity);
    }
    /**
     * 添加
     *
     * @param
     * @return
     */
    @InsertAnnFPA
    @Override
    public TestPlanFpa addPersonalityTestPlan(TestPlanFpa testPlanFpa) {
        while (true) {
         TestPlanFpa   testPlanFpa1 = new TestPlanFpa();
            //此处，生成不重复的随机数，不足6为的前补零
            String sixRandom = MyTools.getSixRandom();
            testPlanFpa1.setTest_code(sixRandom);
            //判断是否跟数据库已经存在的邀请码重复
            List<TestPlanFpa> testPlanFpaList = personalityTestPlanDao.queryPersonalityTestPlanAll(testPlanFpa1);
            log.info("生成的随机数，跟数据库是否一致校验" + sixRandom);
            if (ObjectUtil.isEmpty(testPlanFpaList) || testPlanFpaList.size() == 0) {
                //退出循环之前，将合法的随机数保存到实体对象中testPlanFpa
                testPlanFpa.setTest_code(sixRandom);
                break;
            }
        }
        this.personalityTestPlanDao.addPersonalityTestPlan(testPlanFpa);
        return testPlanFpa;
    }

    @Override
    public int repetitionName(TestPlanFpa testPlanFpa) {
        return personalityTestPlanDao.repetitionName(testPlanFpa);
    }
}
